package flc.ast.dialog;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.ClassificationAdapter;
import flc.ast.bean.ClassificationBean;
import flc.ast.databinding.DialogAlbumClassificationBinding;
import stark.common.basic.base.BaseSmartDialog;

public class ClassificationDialog extends BaseSmartDialog<DialogAlbumClassificationBinding> implements View.OnClickListener {

    private int selPosition;
    private ClassificationAdapter classificationAdapter;
    private List<ClassificationBean> mClassificationBeans=new ArrayList<>();

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    public ClassificationDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_album_classification;
    }

    @Override
    protected void initView(View view) {

        mDataBinding.rvClassificationList.setLayoutManager(new LinearLayoutManager(getContext()));
        classificationAdapter = new ClassificationAdapter();
        mDataBinding.rvClassificationList.setAdapter(classificationAdapter);
        getData();
        classificationAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                classificationAdapter.getItem(selPosition).setSelected(false);
                classificationAdapter.getItem(position).setSelected(true);
                classificationAdapter.notifyDataSetChanged();
                selPosition = position;
                dismiss();
                if (listener != null) {
                    listener.clickItemName(position, classificationAdapter.getItem(position).getName());
                }
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {
        //点击item
        void clickItemName(int position, String name);
    }

    private void getData() {
        mClassificationBeans.add(new ClassificationBean(getContext().getString(R.string.time_classification), false));
        mClassificationBeans.add(new ClassificationBean(getContext().getString(R.string.location_classification), false));
        mClassificationBeans.add(new ClassificationBean(getContext().getString(R.string.label_classification), false));
        mClassificationBeans.get(selPosition).setSelected(true);
        classificationAdapter.setList(mClassificationBeans);
    }
}
